// import { authorities } from '@/api/login'
// import { loginOut } from '../../api/logOut'
import { getToken, getUser, removeToken, removeUser, setToken, setUser } from '../../utils/localStore'
import { asyncRoutes, constantRoutes } from '@/router'
// import { editList, userLoginFun } from '@/utils/loginIndex'
import { encrypt } from '@/utils/aesEdit'
import { Message } from 'element-ui'
import store from '@/store'

const user = {
  state: {
    user: getUser(),
    token: getToken(),
    name: '',
    userName: '',
    avatar: '',
    storeId: -1,
    userId: 0,
    storeName: '',
    roles: [],
    rolesTable: [],
    routes: [],
    addRoutes: [],
    position: -1,
    positionRoles: [],
    areaList: [],
    storeNameList: [],
    appList: [],
    appIdLogin: 'A000010'
  },

  mutations: {
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_User_Name: (state, userName) => {
      state.userName = userName
    },
    SET_Mobile: (state, mobile) => {
      state.mobile = mobile
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_STORE_ID: (state, storeId) => {
      state.storeId = parseInt(storeId)
    },
    SET_USER_ID: (state, userId) => {
      state.userId = userId
    },
    SET_STORE_NAME: (state, storeName) => {
      state.storeName = storeName
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ROLES_TABLE: (state, rolesTable) => {
      state.rolesTable = rolesTable
    },
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_AREA_LIST: (state, areaList) => {
      state.areaList = areaList
    },
    SET_STORENAME_LIST: (state, storeList) => {
      state.storeNameList = storeList
    }
  },

  actions: {

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_USER', '')
        commit('SET_User_Name', '')
        commit('SET_Mobile', '')
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_ROLES_TABLE', [])
        commit('SET_STORE_ID', 0)
        commit('SET_USER_ID', 0)
        commit('SET_STORE_NAME', '')
        commit('SET_AREA_LIST', [])
        removeUser()
        removeToken()
        resolve()
      })
    }
  }
}

export default user
